<?php // $Id: assignment.class.php, v 1.0.0 2011/03/07 10:12:00 pthayer Exp $
require_once($CFG->libdir.'/formslib.php');

define('ASSIGNMENT_STATUS_SUBMITTED', 'submitted');
define('ASSIGNMENT_STATUS_CLOSED', 'closed');

class assignment_seniorproject extends assignment_base {

	function assignment_seniorproject($cmid="staticonly", $assignment=NULL, $cm=NULL, $course=NULL, $groupid=NULL){
		parent::assignment_base($cmid, $assignment, $cm, $course, $groupid);
		$this->type = "seniorproject";
	}

	function view(){
		global $CFG, $USER;

		$edit = optional_param('edit', 0, PARAM_BOOL);
		$saved = optional_param('saved', 0, PARAM_BOOL);

		$context = get_context_instance(CONTEXT_MODULE, $this->cm->id);

		require_capability('mod/assignment:view', $this->context);

		$submission = $this->get_submission();

		if(!has_capability('mod/assignment:submit', $context)){
			$editable = null;
		}else{
			$editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
		}
		$editmode = ($editable and $edit);

		if($editmode){
			if(!has_capability('mod/assignment:submit', $context)){
				print_error('guestnosubmit', 'assignment');
			}
		}

		add_to_log($this->course->id, 'assignment', 'view', "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);

/// prepare form and process submitted data
        $mform = new mod_assignment_online_edit_form();

        $defaults = new object();
        $defaults->id = $this->cm->id;
        if (!empty($submission)) {
            if ($this->usehtmleditor) {
                $options = new object();
                $options->smiley = false;
                $options->filter = false;

                $defaults->text   = format_text($submission->data1, $submission->data2, $options);
                $defaults->format = FORMAT_HTML;
            } else {
                $defaults->text   = clean_text($submission->data1, $submission->data2);
                $defaults->format = $submission->data2;
            }
        }
        $mform->set_data($defaults);

        if ($mform->is_cancelled()) {
            redirect('view.php?id='.$this->cm->id);
        }

        if ($data = $mform->get_data()) {      // No incoming data?
            if ($editable && $this->update_submission($data)) {
                //TODO fix log actions - needs db upgrade
                $submission = $this->get_submission();
                add_to_log($this->course->id, 'assignment', 'upload',
                        'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
                $this->email_teachers($submission);
                //redirect to get updated submission date and word count
                redirect('view.php?id='.$this->cm->id.'&saved=1');
            } else {
                // TODO: add better error message
                notify(get_string("error")); //submitting not allowed!
            }
        }

/// print header, etc. and display form if needed
        if ($editmode) {
            $this->view_header(get_string('editmysubmission', 'assignment'));
        } else {
            $this->view_header();
        }

        $this->view_intro();

        $this->view_dates();

        if ($saved) {
            notify(get_string('submissionsaved', 'assignment'), 'notifysuccess');
        }

        if (has_capability('mod/assignment:submit', $context)) {
            if ($editmode) {
                print_box_start('generalbox', 'online');
                $mform->display();
            } else {
                print_box_start('generalbox boxwidthwide boxaligncenter', 'online');
                if ($submission) {
                    echo format_text($submission->data1, $submission->data2);
                } else if (!has_capability('mod/assignment:submit', $context)) { //fix for #4604
                    echo '<div style="text-align:center">'. get_string('guestnosubmit', 'assignment').'</div>';
                } else if ($this->isopen()){    //fix for #4206
                    echo '<div style="text-align:center">'.get_string('emptysubmission', 'assignment').'</div>';
                }
            }
            print_box_end();
            if (!$editmode && $editable) {
                echo "<div style='text-align:center'>";
                print_single_button('view.php', array('id'=>$this->cm->id,'edit'=>'1'),
                        get_string('editmysubmission', 'assignment'));
                echo "</div>";
            }

        }

        $this->view_feedback();

        $this->view_footer();
	}

	function setup_elements(&$mform) {
		global $CFG, $COURSE;

		$attributes = array('size'=>'20');
		$mform->addElement('text', 'reader2', 'Second Reader', $attributes);
	}
}
